// pages/news/search/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageStatus: false,
    chatUUID: '',
    userInfo: {},
    keyName: '',
    listData:[],
    showName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {
      chatUUID,
      showName,
      isSearch,
      keyName
    } = options
    console.log(keyName);
    this.setData({
      chatUUID,
      keyName,
      showName
    })
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo
    })
    this.search(true)
    console.log(chatUUID, showName, isSearch, keyName);
  },
  // flag 判断是都第一次进入页面 开始搜索
  async search(flag) {
    if (!flag) {
      if (this.data.keyName == '') {
        wx.showToast({
          icon: 'none',
          title: '请输入搜索内容'
        })
        return false
      }
    }

    let searchWords = this.data.keyName
    wx.cloud.init()
    const {
      data: {
        code,
        errorMsg,
        data
      }
    } = await wx.cloud.callContainer({
      "config": {
        "env": "prod-0g38oh8hf0e4d5ac"
      },
      // "path": "/v1/chat/"+this.data.userInfo.mobile+"?searchWords=" + encodeURIComponent(searchWords),
      "path": "/v1/chat/search" + '/' + this.data.chatUUID + "?searchWords=" + encodeURIComponent(searchWords),
      "header": {
        "X-WX-SERVICE": "springboot-069k"
      },
      "method": "get",
      "data": {
        "action": "inc",
      }

    })
    console.log(code, data);
    if (code && code == 1) {
      if (data.chatList.length == 0) {
        wx.showToast({
          icon: 'none',
          title: '未搜索到消息'
        })
        this.setData({
          keyName: ''
        })
      } else {
        console.log();

        this.setData({
          listData: data.chatList,
          // isSearch: true,
          // saveKwd: searchWords
        })

      }
    } else {
      wx.showToast({
        icon: 'none',
        title: '未搜索到消息'
      })
      this.setData({
        keyName: ''
      })
    }
    if (flag) {
      this.setData({
        pageStatus: true
      })
    }
  },


  goDetails(e){
    let {id,showname,positionid,pageno} = e.currentTarget.dataset
    console.log(id,showname,positionid,pageno);
    wx.navigateTo({
      url: '/pages/news/service/index?showName='+this.data.showName+'&chatUUID='+id+'&positionId='+positionid+'&pageno='+pageno+'&isSearch=true',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})